var url = null;

$(function() {
	loadJob();
	loadBranch();
});

/**
 * 增加用户
 */
function initAdd() {
	url = getHttpRequestPath("system/addUser");
	clearData();
	loadJob();
	loadBranch();

	$('#dlg').dialog({
		modal : true
	}).dialog('open').dialog('setTitle', '增加用户');
}

/**
 * 修改用户
 */
function initEdit(userId) {
	url = getHttpRequestPath("system/editUser");
	clearData();
	loadJob();
	loadBranch();
	getUser(userId);
	$('#userId_text').attr("disabled", true);
	$('#dlg').dialog({
		modal : true
	}).dialog('open').dialog('setTitle', '修改用户');
}

function loadJob() {
	$('#job_text').combobox({
		url : getHttpRequestPath("system/loadJobs"),
		valueField : 'value',
		textField : 'name'
	});
	$('#job_query').combobox({
		url : getHttpRequestPath("system/loadJobs"),
		valueField : 'value',
		textField : 'name'
	});
}

function loadBranch() {
	$('#branch_text').combobox({
		url : getHttpRequestPath("system/loadBranchs"),
		valueField : 'value',
		textField : 'name'
	});
	$('#branch_query').combobox({
		url : getHttpRequestPath("system/loadBranchs"),
		valueField : 'value',
		textField : 'name'
	});
}

/**
 * 查询
 */
function query() {
	var cmd = new Cmd();
	var userId = $("#userId_query").val();
	cmd.userId = userId.length > 0 ? userId : null;
	var userName = $("#userName_query").val();
	cmd.userName = userName.length > 0 ? userName : null;
	var jobId = $("#job_query").combobox('getValue');
	cmd.jobId = jobId.length > 0 ? jobId : null;
	var branchId = $("#branch_query").combobox('getValue');
	cmd.branchId = branchId.length > 0 ? branchId : null;
	var isManager = $("#isManager_query").combobox('getValue');
	cmd.isManager = isManager.length > 0 ? new Number(isManager) : -1;
	
	$('#dg').datagrid({
		url : getHttpRequestPath("system/queryUsersList"),
		pageNumber : 1,
		queryParams : {
			cmd : JSON.stringify(cmd, cmd.StringifyReplacer)
		}
	});
}

function clearData() {
	$('#userId_text').val("");
	$('#userId_text').attr("disabled", false);
	$("#userName_text").val("");
	$("#telephone_text").val("");
	$("#job_text").combobox("setValue", "");
	$("#job_text").combobox('enable');
	$("#branch_text").combobox("setValue", "");
	$("#branch_text").combobox('enable');
	$("#isManager_text").combobox("setValue", "");
	$("#isManager_text").combobox('enable');
}

/**
 * 初始化表单数据
 * 
 * @param row
 */
function initUser(user) {
	$("#userId_text").val(user.userId);
	$("#userName_text").val(user.userName);
	$("#telephone_text").val(user.telephone);
	$("#job_text").combobox("setValue", user.jobId);
	$("#branch_text").combobox("setValue", user.branchId);
	$("#isManager_text").combobox("setValue", user.isManager);
}

function formatManager(val, row, index) {
	var str = "";
	if (row.isManager == 1) {
		str = "是";
	} else if (row.isManager == 0) {
		str = "否";
	}
	return str;
}

var formatDateStr = "yyyy-MM-dd hh:mm:ss";
function formatDate(val, row, index) {
	var str = "";
	str += new Date(row.registDate).format(formatDateStr);
	return str;
}

/*
 * 操作
 */
function formatAction(val, row, index) {
	var str = ""; 
	str += createLinkElement('编辑', 'initEdit(\'' + row.userId + '\')', 'icon-edit');
	str += createLinkElement('删除', 'delUser(\'' + row.userId + '\')', 'icon-cancel');
	return str;
}